import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, Validators } from '@angular/forms';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ApiService } from 'src/app/core/api';
import { MessageService } from 'src/app/core/service/message.service';
import { DatePipe } from '@angular/common';
 
/**
 * 首页
 */

@Component({
    templateUrl: './home.html',
    styleUrls: ['./home.scss'],
})
export class HomeComponent implements OnInit {
    pages={
        pageNo:1,           
        pageSize:10,
        title:"",                 // 搜索条件
        beginTime:"",
        endTime:"",
        active:""    
    };
    dataList = [];
    article = {};
    count= {};
    tableLoading = false;
    isVisible = false;
    ngOnInit() {
        this.apiService.post('公告统计数',{}).then((res:any)=>{
            this.count = res.data;
        });
        this.loadList();
    }
    constructor(
        private router: Router,
        private fb: FormBuilder,
        private apiService: ApiService,
        private message: MessageService,
        private nzMessageService: NzMessageService,
        private modal: NzModalService,
        private datePipe:DatePipe
    ) { }
    loadList(){
        this.apiService.post('公告列表',this.pages).then((res:any)=>{
            this.dataList = res.data.list;
        })
    }
    changeFilter(){
        this.pages['pageNo'] = 1;
        this.loadList();
    }
    onChange(value) {
        if(value&&value.length === 2){
            this.pages.beginTime = this.datePipe.transform(value[0],'yyyy-MM-dd');
            this.pages.endTime = this.datePipe.transform(value[1],'yyyy-MM-dd');
            this.changeFilter();
        }else{
            this.pages.beginTime = '';
            this.pages.endTime = '';
            this.changeFilter();
        }
    }
    detail(item){
        this.apiService.post("公告详情",{id:item.id}).then((res:any)=>{
            this.article = res.data.data;
            this.isVisible = true;
        })
        
    }
    handleCancel(){
        this.isVisible = false;
    }
    openEquipment(data){
        this.modal.confirm({
            nzTitle: '系统提示',
			nzContent: '确认发送公告？',
			nzOkText: '确认',
			nzOkType: 'primary',
			nzOnOk: () => {
				return new Promise<void>((resolve, reject) => {
					this.apiService
						.post('发送公告', { id: data.id,active:'Active' })
						.then((res: any) => {
							resolve();
							this.nzMessageService.success('发送公告成功！');
							this.loadList();
						})
						.catch((res) => {
							reject();
							this.nzMessageService.warning(res.message);
						});
				}).catch(() => console.log('Oops errors!'));
			},
			nzCancelText: '取消',
        });
    }
    delEquipment(data){
        this.modal.confirm({
            nzTitle: '系统提示',
			nzContent: '确认发删除公告？',
			nzOkText: '确认',
			nzOkType: 'primary',
			nzOnOk: () => {
				return new Promise<void>((resolve, reject) => {
					this.apiService
						.post('删除公告', { id: data.id })
						.then((res: any) => {
							resolve();
							this.nzMessageService.success('公告已删除！');
							this.loadList();
						})
						.catch((res) => {
							reject();
							this.nzMessageService.warning(res.message);
						});
				}).catch(() => console.log('Oops errors!'));
			},
			nzCancelText: '取消',
        });
    }
}
